package com.example.book_recommend.service.impl;

import com.example.book_recommend.dao.*;
import com.example.book_recommend.entity.*;
import com.example.book_recommend.service.BookService;
import com.example.book_recommend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service("UserService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Autowired
    BookCategoryMapper bookCategoryMapper;
    @Autowired
    BookInfoMapper bookInfoMapper;
    @Autowired
    BookClusterMapper bookClusterMapper;

    @Override
    public User userLogin(User user) {
//        user = userDao.userLogin(user.getUsername(), user.getPassword());
        return user;
    }

    @Override
    public int userRegister(User user) {
        return userMapper.insert(user);
    }

    //可能无用
    @Override
    public boolean verifyToken(String username) {
        return false;
    }

    @Override
    public boolean passwordVerify(String password, String username) {
        if(userMapper.selectByUsername(username)==null){
            return false;
        }
        User user = userMapper.selectByUsername(username);
        if(user.getPassword().compareTo(password) == 0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean changeUserInfo(User user) {
        userMapper.updateByPrimaryKey(user);
        return true;
    }

    @Override
    public List<User> getUserByPage(Integer page, Integer size) {
        return userMapper.selectByPage(page, size);
    }

    @Override
    public int countNumber() {
        return userMapper.countNumber();
    }

    @Override
    public ArrayList<Orders> viewOrders(int uid) {
        ArrayList<Orders> ordersOfUser = new ArrayList<>();
        ArrayList<Orders> allOrders = ordersMapper.selectAll();
        for (Orders order : allOrders){
            if (order.getUserId() == uid){
                ordersOfUser.add(order);
            }
        }
        return ordersOfUser;
    }

    @Override
    public OrderDetail viewDetailedOrders(String orderId) {
        ArrayList<OrderDetail> orderDetails = orderDetailMapper.selectAll();
        OrderDetail orderDetail = new OrderDetail();
        for (OrderDetail orderDetail1 : orderDetails){
            if (orderDetail1.getOrderId().equals(orderId)){
                BookInfo bookInfo = bookInfoMapper.selectByPrimaryKey(orderDetail1.getBookId());
                orderDetail1.setBookName(bookInfo.getName());
                orderDetail1.setImageUrl(bookInfo.getImageUrl());
                orderDetailMapper.updateByPrimaryKey(orderDetail1);
                return orderDetail1;
            }
        }
        return orderDetail;
    }

    @Override
    public ArrayList<HashMap> getBookTypeNum() {
        ArrayList<HashMap> bookTypeNum = new ArrayList<>();
        int zxzjl=0, kxl=0, zzfll=0, jsl=0, jjl=0, whjytyl=0, lsl=0, yywsl=0;
        try {
            ArrayList<BookInfo> bookInfos = bookInfoMapper.getCategories();
            for (BookInfo tempCate : bookInfos){
                if (tempCate!=null) {
                    switch (tempCate.getBookCategoryId()) {
                        case 1:
                            zxzjl++;
                            break;
                        case 2:
                            kxl++;
                            break;
                        case 3:
                            zzfll++;
                            break;
                        case 4:
                            jsl++;
                            break;
                        case 5:
                            jjl++;
                            break;
                        case 6:
                            whjytyl++;
                            break;
                        case 7:
                            lsl++;
                            break;
                        case 8:
                            yywsl++;
                            break;
                    }
                }
            }
        }catch (Exception e){
            System.out.println("表里面有为空的");
            e.printStackTrace();
        }

        HashMap hashMap1 = new HashMap();
        hashMap1.put("哲学宗教类", zxzjl);
        bookTypeNum.add(hashMap1);

        HashMap hashMap2 = new HashMap();
        hashMap2.put("科学类", kxl);
        bookTypeNum.add(hashMap2);

        HashMap hashMap3 = new HashMap();
        hashMap3.put("政治法律类", zzfll);
        bookTypeNum.add(hashMap3);

        HashMap hashMap4 = new HashMap();
        hashMap4.put("军事类", jsl);
        bookTypeNum.add(hashMap4);

        HashMap hashMap5 = new HashMap();
        hashMap5.put("经济类", jjl);
        bookTypeNum.add(hashMap5);

        HashMap hashMap6 = new HashMap();
        hashMap6.put("文化教育体育类", whjytyl);
        bookTypeNum.add(hashMap6);

        HashMap hashMap7 = new HashMap();
        hashMap7.put("历史类", lsl);
        bookTypeNum.add(hashMap7);

        HashMap hashMap8 = new HashMap();
        hashMap8.put("医药卫生类", yywsl);
        bookTypeNum.add(hashMap8);

        return bookTypeNum;
    }

    @Override
    public ArrayList<HashMap> getAgeScopeNum() {
        ArrayList<HashMap> ageScopeNum = new ArrayList<>();
        int l9=0, h10l18=0, h19l30=0, h31l50=0, h51l70=0, h71=0;
        try {
            ArrayList<User> ages = userMapper.selectAllAges();
            for (User tempAge : ages){
                if (tempAge != null){
                    if ((tempAge.getAge() < 10) && (tempAge.getAge() > 0)) {
                        l9++;
                    } else if (tempAge.getAge() < 19) {
                        h10l18++;
                    } else if (tempAge.getAge() < 31) {
                        h19l30++;
                    } else if (tempAge.getAge() < 51) {
                        h51l70++;
                    } else if (tempAge.getAge() > 71) {
                        h71++;
                    }
                }
            }
        }catch (Exception e){
            System.out.println("表里面有年龄为空的");
        }

        HashMap hashMap1 = new HashMap();
        hashMap1.put("<10", l9);
        ageScopeNum.add(hashMap1);

        HashMap hashMap2 = new HashMap();
        hashMap2.put("10~18", h10l18);
        ageScopeNum.add(hashMap2);

        HashMap hashMap3 = new HashMap();
        hashMap3.put("19~30", h19l30);
        ageScopeNum.add(hashMap3);

        HashMap hashMap4 = new HashMap();
        hashMap4.put("31~50", h31l50);
        ageScopeNum.add(hashMap4);

        HashMap hashMap5 = new HashMap();
        hashMap5.put("51~70", h51l70);
        ageScopeNum.add(hashMap5);

        HashMap hashMap6 = new HashMap();
        hashMap6.put(">70", h71);
        ageScopeNum.add(hashMap6);

        return ageScopeNum;
    }

    @Override
    public User getUserInfo(int uid) {
        return userMapper.selectByPrimaryKey(uid);
    }

    @Override
    public Boolean changePassword(int uid, String password) {
        User user = userMapper.selectByPrimaryKey(uid);
        user.setPassword(password);
        userMapper.updateByPrimaryKey(user);
        return true;
    }

    @Override
    public ArrayList<BookInfo> recommend1(Long bookId, int page, int size) {
        int attr1 = bookClusterMapper.selectByPrimaryKey(bookId).getAttr1();
        ArrayList<BookCluster> bookIds = bookClusterMapper.selectByAttr1(attr1, page, size);
        if (bookIds.size() < size){
            bookIds.addAll(bookClusterMapper.selectByRan(size - bookIds.size()));
        }
        ArrayList<BookInfo> books = new ArrayList<>();
        for (BookCluster tempBookId : bookIds){
            books.add(bookInfoMapper.selectByPrimaryKey(tempBookId.getBookId()));
        }
        return books;
    }

    @Override
    public ArrayList<BookInfo> recommend2(Long bookId, int page, int size) {
        int attr2 = bookClusterMapper.selectByPrimaryKey(bookId).getAttr2();
        ArrayList<BookCluster> bookIds = bookClusterMapper.selectByAttr2(attr2, page, size);
        if (bookIds.size() < size){
            bookIds.addAll(bookClusterMapper.selectByRan(size - bookIds.size()));
        }
        ArrayList<BookInfo> books = new ArrayList<>();
        for (BookCluster tempBookId : bookIds){
            books.add(bookInfoMapper.selectByPrimaryKey(tempBookId.getBookId()));
        }
        return books;
    }

    @Override
    public ArrayList<BookInfo> recommendFake(int uid, int page, int size) {
        ArrayList<Orders> ordersOfUser = ordersMapper.selectByUid(uid);
        ArrayList<OrderDetail> orderDetails = new ArrayList<>();
        for (Orders orders : ordersOfUser){
            orderDetails.addAll(orderDetailMapper.selectByOrderId(orders.getOrderId()));
        }
        ArrayList<Long> bookIds = new ArrayList<>();
        for (OrderDetail orderDetail : orderDetails){
            bookIds.add(orderDetail.getBookId());
        }
        ArrayList<BookInfo> booksBought = new ArrayList<>();

        for (Long bookId : bookIds){
            booksBought.add(bookInfoMapper.selectByPrimaryKey(bookId));
        }

        int[] cateCount = {0,0,0,0,0,0,0,0};
        for (BookInfo bookInfo : booksBought){
            cateCount[bookInfo.getBookCategoryId() - 1] ++;
        }
        int maxCate = 1;
        for (int i=0; i<cateCount.length; i++){
            if (cateCount[i] > cateCount[maxCate - 1]){
                maxCate = i+1;
            }
        }
        return bookInfoMapper.selectByCategoryWithOrder(maxCate, page, size);
    }

    @Override
    public User findUserByUsername(String username) {
        return userMapper.selectByUsername(username);
//        return null;
    }

}

